package net.zdsoft.wpchat.support.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 用户所拥有权限实体类
 *
 * @author xuan
 */
public class AuthConfig {
    private Map<String, AuthCatalog> catalogs = null;
    private Map<String, AuthModule> modules = null;
    private Map<String, AuthOperation> operations = null;

    public AuthConfig() {
    }

    public AuthConfig(Map<String, AuthCatalog> catalogs, Map<String, AuthModule> modules,
            Map<String, AuthOperation> operations) {
        this.catalogs = catalogs;
        this.modules = modules;
        this.operations = operations;
    }

    public void setCatalogs(Map<String, AuthCatalog> catalogs) {
        this.catalogs = catalogs;
    }

    public AuthCatalog getCatalog(String catalogId) {
        return catalogs.get(catalogId);
    }

    public void setModules(Map<String, AuthModule> modules) {
        this.modules = modules;
    }

    public AuthModule getModule(String moduleId) {
        return modules.get(moduleId);
    }

    public void setOperations(Map<String, AuthOperation> operations) {
        this.operations = operations;
    }

    public AuthOperation getOperation(String operationId) {
        return operations.get(operationId);
    }

    public List<AuthCatalog> getCatalogs() {
        List<AuthCatalog> list = new ArrayList<AuthCatalog>();
        list.addAll(catalogs.values());
        Collections.sort(list, new Comparator<AuthCatalog>() {
            @Override
            public int compare(AuthCatalog o1, AuthCatalog o2) {
                return o1.getDisplayOrder() - o2.getDisplayOrder();
            }
        });
        return list;
    }

    public List<AuthOperation> getOperations(String catalogId) {
        List<AuthOperation> actions = new ArrayList<AuthOperation>();
        Collection<AuthOperation> collection = operations.values();
        for (Iterator<AuthOperation> iter = collection.iterator(); iter.hasNext();) {
            AuthOperation element = iter.next();
            if (catalogId.equals(element.getModule().getCatalog().getId())) {
                actions.add(element);
            }
        }

        Collections.sort(actions, new Comparator<AuthOperation>() {
            @Override
            public int compare(AuthOperation o1, AuthOperation o2) {
                AuthModule module0 = o1.getModule();
                AuthModule module1 = o2.getModule();
                return module0.getDisplayOrder() - module1.getDisplayOrder();
            }
        });
        return actions;
    }

}
